<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\common\logic\admin;

use app\common\model\RelationTeacherModel;
use app\common\model\RelationStudentModel;
use app\common\model\RelationClassModel;
use app\common\model\RecordClassModel;

class Relation
{
	
	/**
	 * [获取学生列表]
	 * @param  [type]  $param.class_id [班级ID]
	 * @param  [type]  $param    [参数]
	 * @param  integer $page     [description]
	 * @return [type]            [description]
	 */
	public function getStulist($param,$page=20){
		$data = RelationStudentModel::where('class_id',$param['class_id'])
							->order('student_id desc')
							->paginate($page,false,['query'=>$param]);
		return $data;					
	}
	/**
	 * [获取老师下的班组列表]
	 * @param  [type]  $user_id [description]
	 * @param  [type]  $param   [description]
	 * @param  integer $page    [description]
	 * @return [type]           [description]
	 */
	public function getList($user_id,$param,$page=20){
		//如果进行班级查询
		if(!empty($param['p_class_name'])){
				$p_class_id = RelationClassModel::where('class_name',trim($param['p_class_name']))->where('status','in','1,2')->value('class_id');

				$p_class_ids = 0;
				if(!empty($p_class_id)){
					$p_class_id = RelationClassModel::where('p_classid',$p_class_id)->where('status',3)->field('class_id')->select()->toArray();
					dump($p_class_id);
					if(!empty($p_class_id)){
						$array = [];
						foreach ($p_class_id as $key => $value) {
							$array[] = $value['class_id']; 
						}
						$p_class_ids = implode(',', $array);
					}
				}
				
				$param['p_class_ids'] = $p_class_ids;
				
			}

		$data = RelationTeacherModel::where('user_id',$user_id)
							->where(function($query) use($param){
								if(!empty($param['status'])){
									$query->where('status',$param['status']);
								}
								if(isset($param['p_class_ids'])){
									
									$query->where('class_id','in',$param['p_class_ids']);
								}

							})
							->order('teacher_id desc')
							->paginate($page,false,['query'=>$param])
							->each(function($value,$key){
								$value['count'] = RelationStudentModel::where('class_id',$value['class_id'])->count('student_id');
							});

		return $data;
	}
	/**
	 * [创建班级或者组]
	 * @param  [type]  $class_name [班级或者组名称]
	 * @param  [type]  $status     [状态]
	 * @param  [type]  $user_id    [用户ID]
	 * @param  integer $p_classid  [上级班级ID]
	 * @return [type]              [班级ID]
	 */
	public function createClass($class_name,$status,$user_id,$p_classid=0){
			$data = [
					'class_name'=>$class_name,
					'status'=>$status,
					'user_id'=>$user_id,
					'p_classid'=>$p_classid,
					'create_time'=>date('Y-m-d H:i:s')
				];
			$RelationClassModel = new RelationClassModel();
			$rs = $RelationClassModel->save($data);
			return $RelationClassModel->class_id;

	}
	/**
	 * [教师绑定班级]
	 * @param  [type] $class_id [班级ID]
	 * @param  [type] $user_id  [用户ID]
	 * @param  [type] $status   [状态]
	 * @return [type]           [老师ID]
	 */
	public function teacher($class_id,$user_id,$status){
		$data = [
				'class_id' => $class_id,
				'user_id' => $user_id,
				'status' => $status,
			];

		$RelationTeacherModel = new RelationTeacherModel();
		$rs = $RelationTeacherModel->save($data);
		return $RelationTeacherModel->teacher_id;
	}
	/**
	 * [绑定班级和学生的关系]
	 * @param  [type]  $class_id [班级ID]
	 * @param  [type]  $user_ids  [学生ids 数组]
	 * @param  [type]  $status   [状态]
	 * @param  integer $level    [1 组长 0组员]
	 * @return [type]            [description]
	 */
	public function student($class_id,$user_ids,$status,$level=1){
		foreach ($user_ids as $key => $value) {
			$data[$key] = [
					'class_id' => $class_id,
					'user_id' => $value,
					'status' => $status,
					'level' => $level,
				];
		}
		$RelationStudentModel = new RelationStudentModel();
		$rs = $RelationStudentModel->saveAll($data);
		return $rs;
	}
	/**
	 * [班组课题发放记录]
	 * @param  [type]  $param.class_id [课题ID]
	 * @param  [type]  $param    [description]
	 * @param  integer $page     [description]
	 * @return [type]            [description]
	 */
	public function mytask($param,$page=20){
		$data = RecordClassModel::where('class_id',$param['class_id'])
						->order('record_class_id desc')
						->paginate($page,false,['query'=>$param]);
	
		return $data;
	}
	
}